﻿using MediatR;
using PublicClass;
using S.RBAC.Domain;
using S.RBAC.Domain.Dto;
using S.RBAC.Infrastructure.Interface;
using S.RBAC.Read.Api.Application.Command.StaffCommand;

namespace S.RBAC.Read.Api.Application.CommandHandlers.StaffHandler
{
    public class StaffSelectCommandHandler : IRequestHandler<StaffSelectCommand, PageInfo<StaffDto>>
    {
        protected readonly IStaffRepository _staff;
        protected readonly IStaffStateRepository _staffState;
        protected readonly IPostRepository _post;
        protected readonly IStationRepository _station;
        protected readonly IRoleRepository _role;
        protected readonly IDepartmentRepository _department;

        public StaffSelectCommandHandler(IStaffRepository staff, IStaffStateRepository staffState, IPostRepository post, IStationRepository station, IRoleRepository role, IDepartmentRepository department)
        {
            _staff = staff;
            _staffState = staffState;
            _post = post;
            _station = station;
            _role = role;
            _department = department;
        }

        public async Task<PageInfo<StaffDto>> Handle(StaffSelectCommand request, CancellationToken cancellationToken)
        {
            var info = (from a in await _staff.GetAllStaff()
                        join b in await _staffState.GetStaffStates() on a.StaffStateId equals b.StaffStateId
                        join c in await _post.GetAllPosts() on a.PostId equals c.PostId
                        join d in await _station.GetAllStations() on a.StationId equals d.StationId
                        join e in await _role.GetAllRole() on a.RoleId equals e.RoleId
                        join f in await _department.GetAllDepartment() on a.DepartmentId equals f.DepartId
                        where a.IsDel == false
                        select new StaffDto
                        {
                            StaffId = a.StaffId,
                            StaffName = a.StaffName,
                            Gender = a.Gender,
                            EmployeeID = a.EmployeeID,
                            IDNumber = a.IDNumber,
                            Phone = a.Phone,
                            Photograph = a.Photograph,
                            UnitName = a.UnitName,
                            EntryDate = a.EntryDate,
                            AccountNumber = a.AccountNumber,
                            Area = a.Area,
                            Location = a.Location,
                            PostId = c.PostId,
                            PostName = c.PostName,
                            StationId = d.StationId,
                            StationName = d.StationName,
                            StaffStateId = b.StaffStateId,
                            StaffStateName = b.StaffStateName,
                            RoleId = e.RoleId,
                            RoleName = e.RoleName,
                            DepartmentId = f.DepartId,
                            DepartmentName = f.DepartName
                        }).ToList();

            if (!string.IsNullOrWhiteSpace(request.StaffName))
            {
                info = info.Where(t => t.StaffName.Contains(request.StaffName)).ToList();
            }
            if (request.DepartmentId > 0 & request.DepartmentId != null)
            {
                info = info.Where(t => t.DepartmentId == request.DepartmentId).ToList();
            }
            if (request.StaffStateId > 0 & request.StaffStateId != null)
            {
                info = info.Where(t => t.StaffStateId == request.StaffStateId).ToList();
            }

            //总行数
            int totalCount = info.Count;
            //总页数
            int pageCount = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
            //分页
            info = info.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

            return new PageInfo<StaffDto> { totalCount = totalCount, pageCount = pageCount, datas = info };
        }
    }
}
